package com.unknow.first.dapp.admin.account.job;

import com.unknow.first.dapp.admin.account.mapper.AddressAccountDetailMapper;
import lombok.extern.slf4j.Slf4j;
import org.cloud.core.redis.RedisUtil;
import org.cloud.scheduler.constants.MisfireEnum;
import org.cloud.scheduler.job.BaseQuartzJobBean;
import org.jetbrains.annotations.NotNull;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.util.Assert;


/**
 * 扫描日志
 */
@Slf4j
public class BackupAccountDetailJob extends BaseQuartzJobBean {

//    harvest-private-key: "df5e935996920389eb28da2546a719def000155e836187d1d9f95f114fd0f67a"
//    gas-price: "2_000_000"
//    gas-limit: "500_000"


    final private String redisLockerName = "AddressAccount.BackupAccountDetailJob.locker";
    RedisUtil redisUtil;

    private AddressAccountDetailMapper addressAccountDetailMapper;

    public BackupAccountDetailJob(RedisUtil redisUtil, AddressAccountDetailMapper addressAccountDetailMapper) {
        this.redisUtil = redisUtil;
        this.addressAccountDetailMapper = addressAccountDetailMapper;
    }


    @Override
    protected void init() {
        this.jobName = "资金明细备份定时任务";
        jobData.put("description", "资金明细备份定时任务");
        this.jobTime = "0 0 0 * * ? ";
        setMisfire(MisfireEnum.CronScheduleMisfireEnum.MISFIRE_INSTRUCTION_DO_NOTHING);
    }

    @Override
    protected void executeInternal(@NotNull JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Assert.isTrue(redisUtil != null, "redisutil 未获取到");
        if (!redisUtil.getLock(redisLockerName, 60 * 60 * 1000)) {
            return;
        }

        try {
            addressAccountDetailMapper.upBackupAccountDetail();
        } catch (Exception e) {
            log.error("", e);
        } finally {
            redisUtil.releaseLock(redisLockerName);
        }
    }
}